<?php

class Parameter_controller extends CI_Controller {

    function __construct(){
        parent::__construct();
        $this->load->library('session');
        $this->load->helper('url');
        $this->load->helper('utils');
                            
        Validator::is_access($this->session->userdata('user_id'));
    }

    function index($category=null){

        $section = "parameter";
        $title = _LB_PARAMETER;
        $cfer = new Cfer(array(
        $title => base_url('parameter')));
        
        $tabs = array(
            PARAMETER_TYPE_SYSTEM => _LB_SYSTEM,
            PARAMETER_TYPE_DISPLAY => _LB_DISPLAY
        );
        $category = (!in_array($category, array_keys($tabs)))?PARAMETER_TYPE_SYSTEM:$category;
        
        $filter = array();
        $filter['type'] = PARAM_TYPE_LIST_QUERY;
        $parameter = Parameter::getList($filter);
        $references = null;
        while ($parameter->fetchNext()) {
            $references[$parameter->id] = array();

            if ($parameter->extra) {
                foreach ($this->db->query($parameter->extra)->result() as $row) {
                    $references[$parameter->id][$row->id]['value'] = $row->value;

                    if ($row->label) {
                        $references[$parameter->id][$row->id]['label'] = $row->label;
                    }
                }
            }
        }

        $filter['name'] = $this->input->post('name');
        $filter['category'] = $category;
        $filter['type'] = $this->input->post('type');
        $this->data['param'] = Parameter::getList($filter);
        $this->data['filter'] = $filter;
        $this->data['title'] = $title;
        

        $array_menus = array();
        
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);

        $this->data['array_menus'] = $array_menus;
        $this->data['section'] = $section;
        $this->data['cfer'] = $cfer;
        $this->data['tabs'] = $tabs;
        $this->data['category'] = $category;
        $this->data['references'] = $references;
        $this->data['cfer'] = $cfer;
        $this->load->view('parameter_list', $this->data);
    }

    function delete($id = null) {
        $parameter = new Parameter();

        if ($id && !$parameter->get($id)) {
                redirect(base_url('parameter'));
        }
        $parameter->delete();
        redirect(base_url('parameter'));
    }
    
    function form($id = null) {
        $section = "parameter";
		
        if ($id) {
            $cfer = new Cfer(array(
            _LB_PARAMETER => 'parameter',
            _LB_EDIT_PARAM => base_url('parameter/form/'.$id)));
        } else {
            $cfer = new Cfer(array(
            _LB_PARAMETER => 'parameter',
            _LB_ADD_PARAM => base_url('parameter/form/')));
        }
        
        $act = $this->input->get_post('act');
    
        $parameter = new Parameter();

        if ($id && !$parameter->get($id)) {
                redirect(base_url('parameter'));
        }
        
        if ($act == ACT_VALID_EDIT) {
            $parameter->value = $this->input->post('value');
            $parameter->code = $this->input->post('code');
            $parameter->type = $this->input->post('type');
            $parameter->name = $this->input->post('name');
            if ($parameter->validateInput()) {
                if ($id) {
                    $parameter->update();
                } else {
                    $parameter->insert();
                }
                redirect(base_url('parameter'));
            }
        }

        $array_menus = array();
        $filter = array();
        $filter['parent_id'] = 0;
        $filter['type'] = 1;
        Menu::getMenuList($array_menus, $filter);
        
        $title = _LB_PARAMETER;
        
        $tabs = array(
            PARAMETER_TYPE_SYSTEM => _LB_SYSTEM,
            PARAMETER_TYPE_DISPLAY => _LB_DISPLAY
        );
        
        $category = !$this->input->post('category')||
            !in_array($this->input->post('category'), array_keys($tabs))?PARAMETER_TYPE_SYSTEM:$this->input->post('category');
        
        $this->data['parameter'] = $parameter;
        $this->data['title'] = $title;

        $this->data['array_menus'] = $array_menus;
        $this->data['array_param_type'] = array(PARAM_TYPE_TEXTBOX=>_LB_TYPE_TEXTBOX,
                                                PARAM_TYPE_CHECKBOX=>_LB_TYPE_CHECKBOX,
                                                PARAM_TYPE_RADIOBUTTON=>_LB_TYPE_RADIOBUTTON,
                                                PARAM_TYPE_SELECTBOX=>_LB_TYPE_SELECTBOX,
                                                PARAM_TYPE_MULTISELECTBOX=>_LB_TYPE_MULTISELECTBOX,
                                                PARAM_TYPE_TEXTAREA=>_LB_TYPE_TEXTAREA);
        $this->data['backlink'] = base_url('parameter');
        $this->data['section'] = $section;
        $this->data['tabs'] = $tabs;
        $this->data['category'] = $category;
        $this->data['cfer'] = $cfer;
        $this->load->view('parameter_form', $this->data);
    }
}
